package logic

import (
	"context"
	"fmt"
	"shopping-mall/models"

	"shopping-mall/shopping_rpc/internal/svc"
	"shopping-mall/shopping_rpc/shopping_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *shopping_rpc.UpdateUserRequest) (*shopping_rpc.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if models.DB.Where("id = ?", in.UserId).Limit(1).Find(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("未查询到账号")
	}
	user.Name = in.Name
	user.Address = in.Address
	user.HeadImage = in.HeadImage
	if models.DB.Updates(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &shopping_rpc.UpdateUserResponse{}, nil
}
